package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.*;
import com.design.softwaredesign.front.DonationInfo;
import com.design.softwaredesign.mapper.DonationMapper;
import com.design.softwaredesign.service.DonationService;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.DonationVo;
import com.design.softwaredesign.vo.GoodVo;
import com.design.softwaredesign.vo.TimeStampVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/14 17:15
 */
@Service
public class DonationServiceImpl implements DonationService {
    @Autowired
    private DonationMapper donationMapper;

    @Override
    public void addImage(String donationId, String url) {
        DonationImages donationImages = donationMapper.selectDonationImages(donationId);
        url += ";";
        if(donationImages == null) {
            donationImages = new DonationImages();
            donationImages.setDonationId(donationId);
            donationImages.setImageUrl(url);
            donationMapper.addDonationImages(donationImages);
        } else {
            String images = donationImages.getImageUrl();
            images += url;
            donationImages.setImageUrl(images);
            donationMapper.updateImages(donationImages);
        }
    }

    @Override
    public DataVo addDonation(DonationInfo donationInfo) {
        DataVo dataVo = new DataVo();
        Donation donation = new Donation();
        try {
            BeanUtils.copyProperties(donationInfo, donation);
            SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
            String postTime = dateFm.format(new java.util.Date());
            donation.setPostTime(postTime);
            donationMapper.addDonation(donation);
            DonationLog donationLog = new DonationLog(donation.getDonationId(), 1, postTime, 0, postTime, 0, postTime, 0, postTime);
            donationMapper.addDonationLog(donationLog);
            dataVo.setCode(200);
            dataVo.setMsg("捐赠上传成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("捐赠上传失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getDonationList(Integer pageNum, Integer pageSize, String query, String userId) {
        if(pageNum == null) {
            pageNum = 1;
        }
        if(pageSize == null) {
            pageSize = 5;
        }
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取捐赠列表失败");
        List<Donation> donationList = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            donationList = donationMapper.getDonationList(userId,query);
            PageInfo<Donation> goodPageInfo = new PageInfo<>(donationList);
            List<DonationVo> donationVoList = new ArrayList<>();
            Donation donation = null;
            for(int i = 0; i < donationList.size(); i++) {
                DonationVo donationVo = new DonationVo();
                donation = donationList.get(i);
                BeanUtils.copyProperties(donation, donationVo);
                if(!donation.getImageUrl().equals("")) {
                    String []imageUrls = donation.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    donationVo.setImageUrlList(imageUrlList);
                }
                donationVoList.add(donationVo);
            }
            dataVo.setCode(200);
            dataVo.setCount(goodPageInfo.getTotal());
            dataVo.setMsg("获取捐赠列表成功");
            dataVo.setData(donationVoList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return  dataVo;
        }
    }

    @Override
    public DataVo deleteDonationById(String donationId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("删除捐赠失败");
        try {
            if(donationMapper.deleteDonationById(donationId) == 1) {
                donationMapper.deleteDonationImages(donationId);
            }
            dataVo.setCode(200);
            dataVo.setMsg("删除捐赠成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getDonationTimestamp(String donationId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("捐赠物品时间线获取成功");
        try {
            List<TimeStampVo> timeStampVoList = new ArrayList<>();
            DonationLog donationLog = donationMapper.getDonationTimestamp(donationId);
            if(donationLog.getPostState() == 1) {
                TimeStampVo timeStampVo1 = new TimeStampVo(donationLog.getPostTime(), "发布捐赠成功");
                timeStampVoList.add(timeStampVo1);
            }
            if(donationLog.getAcceptStaet() == 1) {
                TimeStampVo timeStampVo2 = new TimeStampVo(donationLog.getAccepTime(), "管理员接收捐赠成功");
                timeStampVoList.add(timeStampVo2);
            }
            if(donationLog.getTransationState() == 1) {
                TimeStampVo timeStampVo3 = new TimeStampVo(donationLog.getTransationTime(), "已向公益对象所在地发件");
                timeStampVoList.add(timeStampVo3);
            }
            if(donationLog.getArrivalState() == 1) {
                TimeStampVo timeStampVo4 = new TimeStampVo(donationLog.getArrivalTime(), "公益对象已成功收到您捐赠的物品");
                timeStampVoList.add(timeStampVo4);
            }
            dataVo.setCode(200);
            dataVo.setMsg("获取捐赠时间线成功");
            dataVo.setData(timeStampVoList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }
}
